home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 May / may_2001.iso / intercd / root / ^Php / php album / album_functions.php next >
Encoding:
PHP Script  |  2001-02-18  |  6.3 KB  |  192 lines

  1. <?php    
  2. /* album_functions.php
  3. * .net magazine (www.netmag.co.uk), issue 83
  4. * Matt Kynaston, 2001
  5. * Distributed under the GNU Public License - www.gnu.org/copyleft/gpl.html
  6. *
  7. * This file contains the PHP functions used in album.php
  8. * I've seperated it out to make editing the album.php simpler, and
  9. * so you can reuse these functions in your own scripts by simply
  10. * including this file along with the constant declarations in album.php
  11. */
  12.  
  13. /* function uploadFile()
  14. * checks to see if file data has been posted, if so
  15. * copies it to photo dir with unique file name and creates thumbnail, then
  16. * prints success or error message
  17. */
  18. function uploadFile() {
  19.     // import form data into local function
  20.     global $ulFile, $ulFile_name, $ulFile_type;
  21.     
  22.     // check for form data
  23.     if (isset($ulFile) && $ulFile_name) {
  24.         // check uploaded file is one of supported types
  25.         if ($ulFile_type == "image/png" ||
  26.                 $ulFile_type == "image/jpeg" ||
  27.                 $ulFile_type == "image/pjpeg") { 
  28.             
  29.             $fn = getUniqueName($ulFile_name); // generate a unique file name
  30.     
  31.             /* move file to the photo directory. Notice we now use move_uploaded_file
  32.              * instead of copy. It includes a check to make sure the file came from
  33.              * the right source, making it more secure
  34.             */
  35.             if (move_uploaded_file($ulFile, PHOT_DIR."/$fn")) {
  36.                 print "<p>$ulFile_name successfully uploaded</p>\n";
  37.                 saveThumbnail($fn); // generate tumbnail image
  38.             } else {
  39.                 print "<p>Oops! Couldn't upload $ulFile_name</p>\n";
  40.                 unlink($ulFile); // remove temporary file
  41.             }
  42.         } else {
  43.             print "<p>Sorry, file type $ulFile_type not supported. Try a JPEG or PNG image.</p>\n";
  44.             unlink($ulFile); // remove temporary file
  45.         }
  46.     }
  47. }
  48.  
  49. /* function getUniqueName(original_filename)
  50. * Returns unique file name. Checks to see if the original file name exists. If so
  51. * searches for next available name by appending counter. Notice the 'ereg' function
  52. * used to split the filename into name and suffix - this uses a regular expression.
  53. * Also note that to join strings in PHP we use '.' instead of JavaScript's "+".
  54. */
  55. function getUniqueName($fn) {
  56.     $counter=1; // counter to generate unique file name 
  57.     ereg("(.+)\.(.+)", $fn, $split_fn); // split file name into name and suffix
  58.     
  59.     // keep looping until a unique name is found
  60.     while (file_exists(PHOT_DIR."/$fn")) {
  61.         $fn = $split_fn[1]."_".$counter.".".$split_fn[2];
  62.         $counter++;
  63.     }
  64.     
  65.     return $fn;
  66. }
  67.  
  68. /* function saveThumbnail(filename)
  69. * generates a thumnail image and saves it to the thumbnail directory
  70. * uses the GD extension to manipulate the images - this can't handle 
  71. * GIF images for licensing reasons: the function uses JPEGs and PNGs.
  72. * If there is an error, or the file type is wrong, it generates a 
  73. * 'no thumbnail available' image instead
  74. */
  75. function saveThumbnail($fn) {
  76.     $fpath = PHOT_DIR."/$fn";
  77.     
  78.     /* GetImageSize returns an array:
  79.      * $info[0] - horizontal size of image in pixels
  80.      * $info[1] - vertical size of image in pixels
  81.      * $info[2] - type of image (1=GIF, 2=JPEG, 3=PNG)
  82.     */
  83.     $info = GetImageSize("$fpath");
  84.     
  85.     // do we need to resize image?
  86.     if ($info[0] > MAX_XY || $info[1] > MAX_XY) {
  87.         // is image landscape?
  88.         if ($info[0] >= $info[1]) {
  89.             $width = MAX_XY;
  90.             $height = $info[1]*MAX_XY/$info[0];
  91.         // or portrait?
  92.         } else {
  93.             $height = MAX_XY;
  94.             $width = $info[0]*MAX_XY/$info[1];
  95.         }
  96.     } else {
  97.         // use original dimensions
  98.         $width = $info[0];
  99.         $height = $info[1];
  100.     }
  101.     
  102.     // create new thumbnail image
  103.     $im = ImageCreate($width, $height);
  104.     
  105.     /* determine image type and load original image. Notice new tests for image
  106.      * types. The GD extension has changed a lot over the years, dropping GIFs
  107.      * and adding PNG support. By testing, we avoid trying to process an image
  108.      * PHP can't deal with
  109.     */
  110.     $im_big = ""; // default is no image
  111.     switch ($info[2]) {
  112.         case 1 :
  113.             if (ImageTypes() & IMG_GIF) // test for GIF support 
  114.                 $im_big = ImageCreateFromGIF("$fpath");
  115.             break;
  116.         case 2 :
  117.             if (ImageTypes() & IMG_JPG) // test for JPEG support 
  118.                 $im_big = ImageCreateFromJPEG("$fpath");
  119.             break;
  120.         case 3 :
  121.             if (ImageTypes() & IMG_PNG) // test for PNG support  
  122.                 $im_big = ImageCreateFromPNG("$fpath");
  123.             break;
  124.     }
  125.     
  126.     if ($im_big) {
  127.         /* resize original image into thumbnail - see PHP Manual for details on
  128.          * the arguements ImageCopyResized takes
  129.         */
  130.         ImageCopyResized($im,$im_big,0,0,0,0,$width,$height,$info[0],$info[1]);
  131.     } else {
  132.         // couldn't load original image, so generate 'no thumbnail' image instead
  133.         $width = 100;
  134.         $height = 100;
  135.               $im = ImageCreate($width, $height); // create a blank image
  136.               $bgc = ImageColorAllocate($im, 0, 0, 0); // background color = black
  137.               $tc  = ImageColorAllocate($im, 255, 255, 255); // text color = white
  138.               ImageFilledRectangle($im, 0, 0, $width, $height, $bgc); // draw rectangle
  139.               ImageString($im, 3, 9, 36, "No thumbnail", $tc); // add text
  140.         ImageString($im, 3, 17, 48, "available", $tc);  
  141.           }
  142.     
  143.     /* save our image in thumb directory - if the second argument is left out,
  144.      * the image gets sent to the browser, as in thumbnail.php
  145.     */
  146.     ImageJPEG($im, THUMB_DIR."/".$fn);
  147.     
  148.     // clean up our working images
  149.     ImageDestroy($im_big);
  150.     ImageDestroy($im);
  151. }    
  152.  
  153. /* function makeAlbumTable()
  154. * writes table filled with thumbnail images linked to full size images
  155. */    
  156. function makeAlbumTable() {
  157.     $col = 0; // column counter
  158.     $dh = opendir(PHOT_DIR); // open photo directory
  159.     
  160.     // start table
  161.     print "<table width='100%' border='0' cellspacing='10'>\n<tr align='center'>\n";
  162.     
  163.     // loop through directory 
  164.     while($file=readdir($dh)) {
  165.         // skip directory files
  166.         if ($file=="." || $file=="..") continue;
  167.         
  168.         // get size for use in image tag
  169.         $info = GetImageSize(THUMB_DIR."/$file");
  170.         
  171.         // write table w/thumbnail linked to full sized image
  172.         print "<td valign=middle><a href='".PHOT_DIR."/$file' target='_blank' onfocus='this.blur()'><img src='".THUMB_DIR."/$file' border=0 width=$info[0] height=$info[1]>";
  173.         print "<br>$file</a></td>\n";
  174.         $col++;
  175.         
  176.         // if max number of columns reached, end table row
  177.         if ($col==NUM_COLS) {
  178.             print "</tr>\n<tr align=center>\n";
  179.             $col=0;
  180.         }
  181.     }
  182.     
  183.     // fill remaining cols with empty cells
  184.     for ($i=$col;$i<NUM_COLS;$i++) {
  185.         print "<td></td>\n";
  186.     }
  187.     
  188.     // end table
  189.     print "</tr></table>";
  190. }
  191.     
  192. ?>